
//var bus=new Vue();

//入炉温度和出炉温度初始化
var zhagangTempreture=[
    {name:'入炉温度',type:'line',lineStyle:'solid',color:'#bd9653'},
    {name:'出炉温度',type:'line',lineStyle:'solid',color:'#00ccfe'},
    {name:'温度',type:'line'}

];

var zhagangTempretureOption={
    tooltip:{
        show:true,
        formatter:function(params){
            var nameArr = [];
            var valueArr = [];
            var str = '';
            for(var i=0;i<params.length;i++){
                nameArr[i]=params[i].seriesName;
                valueArr[i]=Number(params[i].data).toFixed(1);

                str+= nameArr[i]+' : '+valueArr[i]+'<br/>'
            }
            return str
        }
    },
    grid:{
        left:'15%',
        top:'20%'
    },
    yAxis:[
        {
            name:'℃'
        }
    ]
};

var statusChartData=[
    {
        name:'erjia',
        series:[
            {name:'二加预测上',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'二加预测下',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'二加历史',type:'line',lineStyle:'solid',color:'#00ccfe'}
        ]
    },
    {
        name:'yijia',
        series:[
            {name:'一加预测上',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'一加预测下',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'一加历史',type:'line',lineStyle:'solid',color:'#00ccfe'}
        ]
    },
    {
        name:'junre',
        series:[
            {name:'均热预测上',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'均热预测下',type:'line',lineStyle:'dotted',color:'#00ccfe'},
            {name:'均热历史',type:'line',lineStyle:'solid',color:'#00ccfe'}
        ]
    }
];
var statusChartOption={
    tooltip:{
        show:true,
        formatter:function(params){
            var nameArr = [];
            var valueArr = [];
            var str = '';
            for(var i=0;i<params.length;i++){
                nameArr[i]=params[i].seriesName;
                valueArr[i]=Number(params[i].data).toFixed(1);

                str+= nameArr[i]+' : '+valueArr[i]+'<br/>'
            }
            return str
        }
    },
    grid:{
        left:'10%'
    },
    yAxis:[
        {
            name:'X10³',
            axisLabel:{
               formatter:function (value, index) {
                   var val=Math.floor(value/Math.pow(10,3));
                   var val=value;
                   return val;
                } 
            }
        }
    ]
};

var app = new Vue({
    el:"#furnace",
    data:{
        headerMenuList:[
            ['1线轧钢','1'],
            ['2线轧钢','2'],
            ['3线轧钢','3'],
            ['A线轧钢','A'],
            ['B线轧钢','B']
        ],
        zhagang:3,
        historyData:{},
        statusObj:{},
        dataList:[],
        rightlist:[],
        msglist:[],
        cancel:false,
        flag:true,
        zhagangBool:true,
        zhagangChart:null,
        statusChartId:['chart_'+uuid(8,64),'chart_'+uuid(8,64),'chart_'+uuid(8,64)],
        statusChart:[],
        TempVal:{
            // 'yijia':10,
            // 'erjia':20,
            // 'junre':30
        },
        msgType:'all',
        platformChart:null,
        performanceChart:null
    },
    filters:{
        keepTwo:function(val){
            return Number(val).toFixed(1)
        },
        formatterVal:function(val){
            return Number(val/Math.pow(10,3)).toFixed(1);
        },
        keepInteger:function(val){
            return Math.floor(val);
        },
        toCn:function(val){
            switch(val){
                case 'erjia':
                    return '二加';
                break;
                case 'yijia':
                    return '一加';
                break;
                case 'junre':
                    return '均热';
                break;
            }
        },
        // 0：正常运行状态
        // 1：冷批转换为热批
        // 2：轧机已经开始停线
        // 3：轧机将要停线
        // 4：热批转换为冷批
        // 5：轧机已经恢复
        // 6：轧机将要恢复
        // 7：未知问题
        toStatus:function(val){
            switch (val){
                case 0:
                    return '正常生产';
                case 1:
                    return '停线';
            }
        }
    },
    mounted:function(){
        this.$nextTick(function(){
            //console.log(this.statusChartId)
            var _this=this;
            this.getZhagangNum();
            this.updateZhagang();
            this.getJson();
            this.getMessage();
            this.initTempretureChart();
            this.getTempVal();
            this.waveChart();
            this.getTempretureHistoryData('入炉温度','ruluwen');
            this.getTempretureHistoryData('出炉温度','chuluwen');
            this.getMartlineData('入炉温度');
            setTimeout(function(){
                _this.initStatusChart();
                _this.loadStatusChartHistoryData();
                _this.loadStatusChartRealData();
                //_this.initPlatformChart();
            },2000);
            _this.getTempretureRealData('入炉温度','ruluwen');
            _this.getTempretureRealData('出炉温度','chuluwen');
            _this.getZhagangStatus();
            setInterval(function(){
                _this.getTempretureRealData('入炉温度','ruluwen');
                _this.getTempretureRealData('出炉温度','chuluwen');
                _this.getMartlineData('入炉温度');
                _this.getZhagangStatus();
                _this.loadStatusChartRealData();
                _this.getMessage();
                _this.waveChart();
            },3000);

        });
    },
    methods:{
        updateZhagang:function(){
            var item=getStore('zhagang');
            var _this = this;
            if(item){
                _this.zhagang=item[1];

            }
        },
        getZhagangNum:function(){
            var _this=this;
            bus.$on('num',function(num){
                _this.zhagang=num;
                _this.getTempretureHistoryData('入炉温度','ruluwen');
                _this.getTempretureHistoryData('出炉温度','chuluwen');
                _this.initStatusChart();
                _this.loadStatusChartHistoryData();
                _this.getMartlineData('入炉温度');
                _this.getTempVal();
                /*if(_this.zhagang=='1'||_this.zhagang=='A'||_this.zhagang=='2'){
                    _this.zhagangBool = false;
                }else{
                    _this.zhagangBool = true;
                }*/
            });

        },
        initStatusChart:function(){
            var _this=this;
            this.statusChartId.forEach(function(item,index){
                _this.statusChart[index]=new Chart(item,{
                    data:statusChartData[index].series,
                    baseOption:statusChartOption
                })
            });
        },
        getSeriesName:function(progress,type){
            var seriesName='';
            if(type=='history' || type=='realPoint'){
                switch(progress){
                    case 'erjia':
                      seriesName='二加历史';
                      break;
                    case 'yijia':
                      seriesName='一加历史';
                      break;
                    case 'junre':
                      seriesName='均热历史';
                      break;
                } 
            }else if(type=='prediction'){
                switch(progress){
                    case 'erjia':
                      seriesName='二加预测下';
                      break;
                    case 'yijia':
                      seriesName='一加预测下';
                      break;
                    case 'junre':
                      seriesName='均热预测下';
                      break;
                }
            }else if(type=='predictionup'){
                switch(progress){
                    case 'erjia':
                        seriesName='二加预测上';
                        break;
                    case 'yijia':
                        seriesName='一加预测上';
                        break;
                    case 'junre':
                        seriesName='均热预测上';
                        break;
                }
            }

            return seriesName;
        },
        getStatusChartData:function(progress,type,index){
            var _this=this;
            var seriesName=this.getSeriesName(progress,type);
            _this.historyData[progress+'_'+type]=[];
            var success=function(res){
                var data=res.data;
                if(data.length==0)return;
                var axis=getAxis(data);
                if(type=="prediction"){
                    var predictData = axis.dataX;
                    _this.statusChart[index].loadX(axis.dataX)
                }
                if(type=='predictionup'){
                    _this.statusChart[index].loadData(seriesName,axis.dataY)
                }
                if(type=='history'){
                    _this.historyData[progress+'_'+type]=data;
                    /*if(_this.zhagang=='A'||_this.zhagang=='1'||_this.zhagang=='2'){//没有预测以历史数据为X轴
                        _this.statusChart[index].loadX(axis.dataX)
                    }*/

                }
                //if(type=="baseLine"){
                //   _this.statusChart[index].loadBaseLine(seriesName,{
                //        data:data
                //   }); 
                // }
                if(type=='realPoint'){
                    if(!_this.historyData[progress+'_history']) return;
                    _this.historyData[progress+'_history'].shift();
                    _this.historyData[progress+'_history'].push(data); 
                    axisreal=getAxis(_this.historyData[progress+'_history']);
                   /* if(_this.zhagang=='A'||_this.zhagang=='1'||_this.zhagang=='2'){//没有预测以历史数据为X轴
                        _this.statusChart[index].loadX(axisreal.dataX)
                    }*/
                    _this.statusChart[index].loadData(seriesName,axisreal.dataY)
                }else{
                    _this.statusChart[index].loadData(seriesName,axis.dataY)
                }
               
                //console.log(res.data);
            };
            var error=function(e){
                console.log('加载轧钢图表状态数据出错了');
            };
            //测试用'../data/'+progress+'_'+type+'.json'
            //URL+'/zhagang/3/erjia/history'
            //progress:erjia,yijia,junre
            this.$http.get(URL+'/zhagang/'+this.zhagang+'/'+progress+'/'+type).then(success,error);
        },
        loadStatusChartHistoryData:function(){
            for(var i=0;i<statusChartData.length;i++){
                var name=statusChartData[i].name;
                this.getStatusChartData(name,'history',i);
            }
        },
        loadStatusChartRealData:function(){
            var _this = this;
            for(var i=0;i<statusChartData.length;i++){
                var name=statusChartData[i].name;
                this.getStatusChartData(name,'predictionup',i);
                this.getStatusChartData(name,'prediction',i);
                this.getStatusChartData(name,'realPoint',i);
                /*if(_this.zhagang=='1'||_this.zhagang=='2'||_this.zhagang=='3'){
                    this.getStatusChartData(name,'prediction',i);
                }*/
            }
        },
        initTempretureChart:function(){
            //实例化轧钢
            this.zhagangChart=new Chart('zhagangTempreture',{
                type:'line',
                data:zhagangTempreture,
                baseOption:zhagangTempretureOption
            });
        },
        getTempretureHistoryData:function(chartName,ajaxName){
            var _this=this;
            var success=function(res){
                var data=res.data;
                this.historyData[chartName]=data;
                var Axis=getAxis(data);
                if(chartName=='入炉温度'){
                  _this.zhagangChart.loadX(Axis.dataX);  
                }
                this.zhagangChart.loadData(chartName,Axis.dataY);
            };
            var error=function(e){
                console.log('轧钢历史加载出错了');
            };
            this.$http.get(URL+'/zhagang/'+this.zhagang+'/'+ajaxName+'/history').then(success,error);
        },
        getTempretureRealData:function(chartName,ajaxName){
            var _this=this;
            var success=function(res){
                var data=res.data;
                if(!this.historyData[chartName])return;
                this.historyData[chartName].shift();
                this.historyData[chartName].push(data);
                var Axis=getAxis(this.historyData[chartName]);
                if(chartName=='入炉温度'){
                  _this.zhagangChart.loadX(Axis.dataX);  
                }
                this.zhagangChart.loadData(chartName,Axis.dataY);
            };
            var error=function(e){
                console.log('轧钢温度加载出错了');
            }
            this.$http.get(URL+'/zhagang/'+this.zhagang+'/'+ajaxName+'/realPoint').then(success,error);
        },
        getMartlineData:function(chartName){
            var _this = this;
            var success = function(res){
                var data = res.data[0];
                if(!this.historyData[chartName])return;
                var MarkData=arrtoJson(data,'hm');
                _this.zhagangChart.loadMarkline(MarkData);
            };
            var error = function(e){
                console.log('标线加载出错了');
            }
            this.$http.get(URL+'zhagang/changeTemperature',{ids:this.zhagang,start:'',end:''}).then(success,error);
        },
        getJson:function(){
            var _this=this;
            this.$http(URL+'/zhagang/xietong').then(function(res){
                _this.dataList= res.data;
            })
        },
        getZhagangStatus:function(){
            var _this=this;
            var success=function(res){
                var data=res.data;
                //_this.statusObj=eval('('+data+')');
                _this.statusObj=data;  
            };
            var error=function(e){
                console.log('轧钢状态加载错了');
            };

            this.$http.get(URL+'/zhagang/'+this.zhagang+'/status').then(success,error);
        },
        getTempVal:function(){
            var _this=this;
            var success=function(res){
                var data=res.data;
                if(data=='')return;
                _this.TempVal=data;
              // _this.TempVal=eval('('+data+')');
            };
            var error=function(e){
                console.log('获取炉顶温度出错了');
            };
           this.$http.get(URL+'/zhagang/'+this.zhagang+'/getValue').then(success,error);

        },
        sendTemVal:function(){
            var _this = this;
            var data = {
                id:_this.zhagang,
                erjia:_this.TempVal.erjia,
                erjia_delta:_this.TempVal.erjia_delta,
                yijia:_this.TempVal.yijia,
                yijia_delta:_this.TempVal.yijia_delta,
                junre:_this.TempVal.junre,
                junre_delta:_this.TempVal.junre_delta
            };
            var success=function(res){
                console.log('成功了')
            };
            var error=function(e){
                console.log('发送出错了');
            };
            this.$http.post(URL+'/zhagang/setValue',data)
        },
        //获取消息中心的数据
        getMessage:function(){
            var _this=this;
            bus.$on('msgType',function(val){
                _this.msgType=val;
            });
            var success=function(res){
                //_this.msglist=eval('('+res.data+')');
                _this.msglist=res.data;    
            };
            var error=function(e){
                console.log('加载轧钢消息中心出错了');
            };
            this.$http(URL+'/message/current').then(success,error);

        },
        waveChart:function(){
            /*波动归因*/
            Chart3('zhagang_performance',URL+'/coalgas/quanwang/bodongguiyin');
        }
    }
});






